package org.argetr.resim.proc;


import javax.media.jai.PlanarImage;

import org.argetr.resim.proc.data.ConstDA;
import org.argetr.resim.proc.data.Data;
import org.argetr.resim.proc.data.ExtremaDA;
import org.argetr.resim.proc.data.HistogramDA;
import org.argetr.resim.proc.data.ImageDA;


public abstract class Processor {
	Data[] _in, _out;
	ConnectionRule _connectionRule = new ConnectionRule();
	private static int _procCount = 0;
	private int _id = 0;
	public Processor() {
		_procCount++;
		setId(_procCount);
		setConnectionRules(_connectionRule);
		_in = new Data[getConnectionRule().getNoInputs()];
		_out = new Data[getConnectionRule().getNoOutputs()];
	}
	public int getId(){
		return _id;
	}
	public ConnectionRule getConnectionRule(){
		return _connectionRule;
	}
	public void setInput(Data in) {
		_in[0] = in;
	}
	public void setInput(Data in, int index) {
		_in[index] = in;
	}
	public void setInputs(Data[] in) {
		_in = in;
	}
	public Data getInput() {
		return _in[0];
	}
	public Data[] getInputs() {
		return _in;
	}
	public Data getOutput() {
		return _out[0];
	}
	public Data[] getOutputs() {
		return _out;
	}
	public Data getOutput(int index) {
		return _out[index];
	}
    public abstract Data process();
    public Data process (Data in){
		setInput(in);
		process();
		return getOutput();
    }
    public Data[] process (Data[] in){
		setInputs(in);
		process();
		return getOutputs();
    }
	private void setId(int id){
		_id = id;
	}
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(ImageDA.class, 0);
		connRule.addOutType(ImageDA.class);
	}
	protected Data getInput(int inputIndex){
	   return _in[inputIndex];
	}
    protected PlanarImage getInputAsPlanarImage(int inputIndex){
       return (PlanarImage)getInput(inputIndex).getData();
    }
    protected PlanarImage getInputAsPlanarImage(){
        Data data = getInput();
        return (PlanarImage)data.getData();
    }
    protected ImageDA getInputAsImageDA(int inputIndex){
        return (ImageDA)getInput(inputIndex);
    }
    protected ImageDA getInputAsImageDA(){
         return (ImageDA)getInput();
    }
    protected ConstDA getInputAsConstDA(int inputIndex){
        return (ConstDA)getInput(inputIndex);
    }
    protected ConstDA getInputAsConstDA(){
         return (ConstDA)getInput();
    }
    protected HistogramDA getInputAsHistogramDA(){
        return (HistogramDA)getInput();
    }
    protected ExtremaDA getInputAsExtremaDA(){
        return (ExtremaDA)getInput();
    }

	protected void setOutput(Data out) {
		_out[0] = out;
	}
}
